#pragma once

#include <optional>
#include <system_error>

#include "helper/sqlite/Value.h"
#include "helper/sqlite/IResult.h"


namespace helper {
namespace sqlite {

//auto && rs = stmt.query();
//while(rs){
//  ...
//  rs.next();
//}
class QueryResult : public IResult{
public:
  static QueryResult make_error(const std::error_code & ec) {
    return QueryResult(ec, nullptr);
  }
  static QueryResult make_error(const std::errc & errc) {
    return QueryResult(std::make_error_code(errc), nullptr);
  }


  QueryResult(const std::error_code & ec, void * stmt);
  virtual ~QueryResult();

  QueryResult(QueryResult && other);
  QueryResult & operator= (QueryResult && other);

  QueryResult(const QueryResult & ) = delete;
  QueryResult & operator= (const QueryResult & ) = delete;

  inline operator bool () const{
    return isOk();
  }

  bool isOk() const override;


  std::optional<Value> values(int index) const;
  void next();
  int columnCount() const;

protected:
  int fetchCode() const override;
private:
  void * _stmt;
};

}//namespace sqlite
}//namespace helper
